package com.lei.interceptor;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.support.RequestContextUtils;

public class UrlInterceptor extends HandlerInterceptorAdapter {

	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		LocaleResolver lr = RequestContextUtils.getLocaleResolver(request);
		String lang = lr.resolveLocale(request).getLanguage();
		
		if ("fr".equals(request.getAttribute("lang"))) {
			if ("en".equals(lang)) {
				lr.setLocale(request, response, Locale.CANADA_FRENCH);
			}
		} else if ("en".equals(request.getAttribute("lang"))) {
			if ("fr".equals(lang)) {
				lr.setLocale(request, response, Locale.CANADA);
			}
		}
		
		return true;
	}
	
}
